package com.jk.bggoods.service;

import com.alibaba.fastjson.JSONArray;
import com.jk.bggoods.dao.GoodsDao;
import com.jk.bggoods.entity.*;
import com.jk.bggoods.pojo.GoodsEntity;
import com.jk.damaicommon.entity.DistrEntity;
import com.jk.damaicommon.wlentity.SessionSiteEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class GoodsService {
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private RedisTemplate redisTemplate;

    public HashMap<String, Object> selectAllGoods(GoodsEntity goods) {
        HashMap<String, Object> map = new HashMap<>();
         List<GoodsEntity> list = goodsDao.selectAllGoods(goods);
        Integer count = goodsDao.getGoodsCount(goods);
        map.put("total",count);
        map.put("rows",list);
        return map;
    }

    public void downGoods(Integer goodsId) {
        goodsDao.downGoods(goodsId);
    }

    public void upGoods(Integer goodsId) {
        goodsDao.upGoods(goodsId);
    }

    public GoodsEntity selectOneGoods(Integer goodsId) {
        return goodsDao.selectOneGoods(goodsId);
    }
    //王乐商品详情
    public GoodsDetailEntity getGoodsById(Integer goodsId) {
        GoodsDetailEntity goods = goodsDao.getGoodsById(goodsId);
        return goods;
    }
    //王乐城市动态下拉
    public List<CityEntity> getCitySelect() {
        List<CityEntity> list = goodsDao.getCitySelect();
        return list;
    }
    //类型动态下拉
    public List<GoodsTypeEntity> getTypeSelect() {
        List<GoodsTypeEntity> list = goodsDao.getTypeSelect();
        return list;
    }
    //系列活动 动态下拉
    public List<GoodsActiveEntity> getActiveSelect() {
        List<GoodsActiveEntity> list = goodsDao.getActiveSelect();
        return list;
    }
    // 添加商品
  /*  public void addgoods(GoodsDetailEntity goods) {

    }*/
    // 回收站
    public HashMap<String, Object> recycleSelectMap(GoodsEntity goods) {
        HashMap<String, Object> map = new HashMap<>();
        List<GoodsEntity> list = goodsDao.recycleSelectAll(goods);
        Integer count = goodsDao.getRecycleCount(goods);
        map.put("total",count);
        map.put("rows",list);
        return map;
    }
    //彻地删除
    public void deleteThisGoods(Integer goodsId) {
        goodsDao.deleteThisGoods(goodsId);
    }
    //新增商品
    public void addGoods(GoodsDetailEntity goods) {
        //新增商品
        goodsDao.addGoods(goods);
        //新增场次
        // goodsDao.addGoodsSeaaion(goods);
        List<GoodsSessionEntity> lists= goods.getGoodsSessionEntityList();
        for (GoodsSessionEntity session:lists ) {
            session.setGoodsId(goods.getGoodsId());
            //新增场次
            goodsDao.addGoodsSeaaion(session);
            List<GoodsLeaveEntity> listl = session.getGoodsLeaveList();
            for (GoodsLeaveEntity leave: listl) {
                leave.setSessionId(session.getSessionId());
                //新增档次
                goodsDao.addGoodsLeave(leave);
            }
        }
        //新增场次
       // goodsDao.addGoodsSeaaion(goods);
        //求开始,结束时间
        GoodsDetailEntity goods2 =  goodsDao.getGoodsTime(goods);
        //修改开始结束时间
        goodsDao.updateGoodsTime(goods2);
        //关联配送方式
        goodsDao.addDistr(goods);

    }
    //获取配送方式
    public String getDistr() {
        List<DistrEntity> list =  goodsDao.getDistr();
        String str = "";
        DistrEntity[] array = list.toArray(new DistrEntity[0]);
        for (int i =0;i<array.length;i++){
            str+="<input type='checkbox' name='distrList["+i+"].distrId' value='"+array[i].getDistrId()+"'/>"+array[i].getDistrMode();
        }
        return str;
    }
    // 新增系列活动
    public void addActive(ActiveEntity activeEntity) {
        goodsDao.addActive(activeEntity);
    }
    //座位管理场次列表
    public List<SessionSiteEntity> getSessionList(SessionSiteEntity sessionSite) {
        List<SessionSiteEntity> list = goodsDao.getSessionList(sessionSite);
        return list;
    }
    //档次动态下拉
    public List<GoodsLeaveEntity> getLeaveSelect(Integer id) {
        List<GoodsLeaveEntity> list = goodsDao.getLeaveSelect(id);
        return list;
    }
    //提交座位表
    public void saveSite(SessionSiteEntity site) {
        //查商品id
        Integer goodsId = goodsDao.getGoodsId(site.getSessionId());
        site.setGoodsId(goodsId);
        //存mysql
        goodsDao.saveSite(site);
        //存redis
        String siteInfo = site.getSiteInfo();
        redisTemplate.opsForHash().put("dmsite","site"+site.getSessionId(),siteInfo);
    }
    //获取座位信息
    public String getSiteInfo(Integer id) {
        String siteInfo = (String) redisTemplate.opsForHash().get("dmsite","site"+id);
        String siteInfo2 =goodsDao.getSite(id);
        return siteInfo2;
    }
}
